package com.example.qihuibao.utils

import com.tencent.mmkv.MMKV
import java.lang.Exception

/**
 *@author: cww
 *@desc:
 *@date: 4/21/23 9:51 PM
 */
object MMKVUtil {
    private val mmkv: MMKV = MMKV.defaultMMKV()

    fun <T>setValue(key:String, value:T) {
        when(value) {
            is Boolean -> mmkv.encode(key, value)
            is Int -> mmkv.encode(key, value)
            is Float -> mmkv.encode(key, value)
            is Double -> mmkv.encode(key, value)
            is Long -> mmkv.encode(key, value)
            is String -> mmkv.encode(key, value)
            else -> throw Exception("未处理的类型")
        }
    }

    fun <T>getValue(key:String, default:T): T {
        return when(default) {
            is Boolean -> mmkv.decodeBool(key, default) as T
            is Int -> mmkv.decodeInt(key, default) as T
            is Float -> mmkv.decodeFloat(key, default) as T
            is Double -> mmkv.decodeDouble(key, default) as T
            is Long -> mmkv.decodeLong(key, default) as T
            is String -> mmkv.decodeString(key, default) as T
            else -> throw Exception("未处理的类型")
        }
    }
}
